﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using jmetal.core;
using jmetal.encodings.solutionType;

namespace jmetal.problems
{
    class LOTZ : Problem
    {
        public LOTZ(string solutionType)
        {
            numberOfVariables_ = 100;
            numberOfObjectives_ = 2;
            numberOfConstraints_ = 0;
            problemName_ = "LOTZ";

            lowerLimit_ = new double[numberOfVariables_];
            upperLimit_ = new double[numberOfVariables_];
            for (int i = 0; i < numberOfVariables_; i++)
            {
                lowerLimit_[i] = 0.0;
                upperLimit_[i] = 1.0;
            }

            solutionType_ = new BinarySolutionType(this);
        }

        public override void evaluate(Solution solution)
        {
            // <pex>
            if (solution == (Solution)null)
                throw new ArgumentNullException("solution");
            if (solution.variable_ == (Variable[])null)
                throw new ArgumentNullException("solution");
            // </pex>
            Variable[] x = solution.variable_;
            double[] fx = new double[numberOfObjectives_];

            bool onesBest = true;
            for (int objective = 0; objective < numberOfObjectives_; objective++)
            {
                double sum = 100;
                for (int i = 0; i < numberOfVariables_; i++)
                {
                    int prod = 1;
                    if (onesBest)
                    {
                        for (int j = 0; j <= i; j++)
                        {
                            prod *= (int)x[j].value_;
                        }
                        sum = sum + prod;
                    }
                    else
                    {
                        for (int j = i; j < numberOfVariables_; j++)
                        {
                            prod *= (int)(1 - x[j].value_);
                        }
                        sum = sum + prod;
                    }
                }
                onesBest = !onesBest;// switch the function between
                fx[objective] = 1.0 / (sum/100);
                // problem
            }

            solution.objective_[0] = fx[0];
            solution.objective_[1] = fx[1];
        }
    }
}
